// src/views/login/AdminLogin.jsx

import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Button, Form, Input, message, Checkbox } from 'antd';
import { adminAuth } from '../../apis/auth'; // 修改引入路径
import './AdminLogin.less';  // 引入 Less 文件

const AdminLogin = () => {
    const [loading, setLoading] = useState(false);
    const navigate = useNavigate();

    const onFinish = async (values) => {
        setLoading(true);
        try {
            const response = await adminAuth.login(values); // 使用 adminAuth.login
            if (response.data.code === 201) {
                message.success('登录成功');
                localStorage.setItem('token', response.data.data.token);
                navigate('/admin');
            } else {
                message.error(response.data.message);
            }
        } catch (error) {
            message.error('登录失败');
        } finally {
            setLoading(false);
        }
    };

    return (
        <div className="container">
            <Form
                name="admin-login"
                className="form"
                initialValues={{ remember: true }}
                onFinish={onFinish}
                onFinishFailed={(errorInfo) => console.log('Failed:', errorInfo)}
                autoComplete="on"
            >
                <div className="title">重邮云超市管理后台</div>

                <Form.Item
                    label=""
                    name="account"
                    rules={[{ required: true, message: '请输入用户名' }]}
                >
                    <Input placeholder='请输入账号' />
                </Form.Item>

                <Form.Item
                    label=""
                    name="password"
                    rules={[{ required: true, message: '请输入密码' }]}
                >
                    <Input.Password placeholder='请输入密码' />
                </Form.Item>

                <Form.Item name="remember" valuePropName="checked">
                    <Checkbox>记住密码</Checkbox>
                </Form.Item>

                <Form.Item className="buttons">
                    <Button
                        type="primary"
                        htmlType="submit"
                        loading={loading}
                        className="login-button"
                    >
                        登录
                    </Button>
                    <Button
                        type="primary"
                        onClick={() => navigate('/admin/register')}
                        className="register-button"
                    >
                        注册
                    </Button>
                </Form.Item>
            </Form>
        </div>
    );
};

export default AdminLogin;

